﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using System.Web.UI.WebControls;
using CUSTOMS.Modules.QLHS.Components;
using System.Data;
using System.Collections;
using CUSTOMS.Modules.QLHS.Components.QLBB;
using System.Collections.Specialized;
using CUSTOMS.Components;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Helpers;

namespace CUSTOMS.Modules.QLHS.Controls
{
    public partial class ChiTietBienBan : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Hashtable settings = this.ParentControl.Settings;
                if (Session["HangHoa"] != null)
                {
                    Session.Remove("HangHoa");
                }

                txtMaDonViHaiQuanChuyenDen.Attributes.Add("onchange", string.Format("handler({0}, {1})", txtMaDonViHaiQuanChuyenDen.ClientID, ddlDonViHaiQuanChuyenDen.ClientID));
                ddlDonViHaiQuanChuyenDen.Attributes.Add("onchange", string.Format("handler({1}, {0})", txtMaDonViHaiQuanChuyenDen.ClientID, ddlDonViHaiQuanChuyenDen.ClientID));
                txtMaDonViHaiQuanChuyenDen.Text = ddlDonViHaiQuanChuyenDen.SelectedValue = settings["MaHaiQuanChuyenDen"].ToString();

                for (int d = 0; d < 24; d++)
                {
                    ddlGio.Items.Add(new ListItem(d.ToString(), d.ToString()));
                    ddlGioXuatPhat.Items.Add(new ListItem(d.ToString(), d.ToString()));
                }

                for (int m = 0; m < 60; m++)
                {
                    ddlPhut.Items.Add(new ListItem(m.ToString(), m.ToString()));
                    ddlPhutXuatPhat.Items.Add(new ListItem(m.ToString(), m.ToString()));
                }

                DateTime now = DateTime.Now;
                txtNgay.Text = txtNgayXuatPhat.Text = now.ToShortDateString();

                ddlGio.SelectedValue = now.Hour.ToString();
                ddlPhut.SelectedValue = now.Minute.ToString();

                IDataReader reader = DanhMuc.GetDonViHaiQuan(false);
                ddlDonViHaiQuanChuyenDen.DataSource = reader;
                ddlDonViHaiQuanChuyenDen.DataBind();
                reader.Close();

                if ((string.IsNullOrEmpty(Request.QueryString["SoToKhai"]) == false) && (string.IsNullOrEmpty(Request.QueryString["MaHaiQuan"]) == false) && (string.IsNullOrEmpty(Request.QueryString["MaLoaiHinh"]) == false) && (string.IsNullOrEmpty(Request.QueryString["NamDangKy"]) == false))
                { 
                    int sotokhai = Convert.ToInt32(Request.QueryString["SoToKhai"]);
                    string mahaiquan = Request.QueryString["MaHaiQuan"];
                    string maloaihinh = Request.QueryString["MaLoaiHinh"];
                    int namdangky =  Convert.ToInt32(Request.QueryString["NamDangKy"]);
                    BienBan bienban = BienBan.Load(sotokhai, mahaiquan, maloaihinh, namdangky);
                    if (bienban != null)
                    {
                        //txtSoToKhai.Text = bienban.SoToKhai;
                        //txtMaHaiQuan.Text = bienban.MaHaiQuan;
                        //txtMaLoaiHinh.Text = bienban.MaLoaiHinh;
                        //txtNamDangKy.Text = bienban.NamDangKy;
                        txtMaDoanhNghiep.Text = bienban.MaDoanhNghiep;
                        //txtPhanLuong.Text = bienban.PhanLuong;
                        //txtSoTo.Text = bienban.SoTo;
                        ddlTruongHop.SelectedValue = bienban.TruongHop;
                        txtMaDonViHaiQuanChuyenDen.Text = ddlDonViHaiQuanChuyenDen.SelectedValue = bienban.MaHaiQuanChuyenDen;
                        //txtNoiGiao.Text = bienban.NoiGiao;
                        //txtNguoiGiao.Text = bienban.NguoiGiao;
                        txtMaDoanhNghiep.Text = bienban.MaDoanhNghiep;
                        txtNguoiDaiDien.Text = bienban.NguoiNhan;
                        // Ngay ban giao / Thoi gian lam bien ban.
                        ddlGio.SelectedValue = bienban.NgayBanGiao.Hour.ToString("N0");
                        ddlPhut.SelectedValue = bienban.NgayBanGiao.Minute.ToString("N0");
                        txtNgay.Text = bienban.NgayBanGiao.ToShortDateString();
                        //txtCanBoTheoDoi.Text = bienban.CanBoTheoDoi;
                        //txtTrangThai.Text = bienban.TrangThai;
                        txtGhiChu.Text = bienban.GhiChu;
                        txtSoBienBan.Text = bienban.SoBienBan;
                        txtSoVanTaiDon.Text = bienban.SoVanTaiDon;
                        txtSoBanChinhToKhai.Text = bienban.SoBanChinhToKhai.ToString("N0");
                        txtSoBanChinhDonChuyenCuaKhau.Text = bienban.SoBanChinhDonChuyenCuaKhau.ToString("N0");
                        txtSoBanSaoVanTaiDon.Text = bienban.SoBanSaoVanTaiDon.ToString("N0");
                        txtSoBanChinhBanKeChiTiet.Text = bienban.SoBanChinhBanKeChiTiet.ToString("N0");
                        txtMoTaHangHoa.Text = bienban.MoTaHangHoa;
                        txtTinhTrangHangHoa.Text = bienban.TinhTrangHangHoa;
                        txtBienBanVeTinhTrangHangHoa.Text = bienban.BienBanVeTinhTrangHangHoa;
                        txtTuyenDuongVanChuyen.Text = bienban.TuyenDuongVanChuyen;
                        txtChieuDai.Text = bienban.ChieuDai.ToString();
                        txtThoiGianVanChuyen.Text = bienban.ThoiGianVanChuyen.ToString();
                        // Thoi gian xuat phat.
                        txtNgayXuatPhat.Text = bienban.GioXuatPhat.ToShortDateString();
                        ddlGioXuatPhat.SelectedValue = bienban.GioXuatPhat.Hour.ToString("N0");
                        ddlPhutXuatPhat.SelectedValue = bienban.GioXuatPhat.Minute.ToString("N0");
                        txtCacVanDeLienQuan.Text = bienban.CacVanDeLienQuan;
                        //txtNguoiNhapDuLieu.Text = bienban.NguoiNhapDuLieu;
                        //txtNgayNhapDuLieu.Text = bienban.NgayNhapDuLieu;

                        DataSet hanghoa = HangHoa.SelectBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy(bienban.SoToKhai, bienban.MaHaiQuan, bienban.MaLoaiHinh, bienban.NamDangKy);
                        gvHangHoa.DataSource = hanghoa;
                        gvHangHoa.DataBind();
                    }
                }
            }
        }
    }
}